<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/message.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/task.jsp"%>
<%
    // 创建日程
    String id = input.getDesc("id", "-1");
    Integer uid = token.getUid();
    List<Arrangement> list = QBuilder.db("basedb").select().from("Arrangement").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("uid=", uid).and("id=", id)).query().getResultObj();
    if (isEmpty(list)) {
        msg = new Message(30041, "日程已不存在");
        Tools.encryptPrint(out, msg);
        return;
    }
    Arrangement ag = list.get(0);
    String team = ag.getTeam();
    List<Arrangement> ags = QBuilder.db("basedb").select().from("Arrangement").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("team=", team)).query().getResultObj();
    if (isEmpty(ags)) {
        msg = new Message(30041, "暂无日程");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
    String patientsStr = input.getDesc("patients", "");
    List patients = new ArrayList();
    if (!isEmpty(patientsStr)) {
        String[] strs = patientsStr.split("\\|");
        for (int  i = 0; i < strs.length; i++) {
            patients.add(strs[i]);
        }
    }
    String doctorStr = input.getDesc("doctors", "");
    List doctors = new ArrayList();
    if (!isEmpty(doctorStr)) {
        String[] strs = doctorStr.split("\\|");
        for (int  i = 0; i < strs.length; i++) {
            doctors.add(strs[i]);
        }
    }
    List<Integer> uids = new ArrayList<Integer>();
    uids.add(uid);
    List<Arrangement> ages = new ArrayList<Arrangement>();
    for (int i = 0; i < ags.size(); i++) {
        ag = ags.get(i);
        Integer partner = ag.getPartner();
        boolean _patients = "Y".equals(ag.getPatients()) ? true : false;
        boolean _doctors = "Y".equals(ag.getDoctors()) ? true : false;
        for (int j = 0; _patients && j < patients.size(); j++) {
            if ((partner+"").equals(patients.get(j))) {
                ag.setStatus(STATUS_DISABLE);
                uids.add(partner);
                ages.add(ag);
                break;
            }
        }
        for (int j = 0; _doctors && j < doctors.size(); j++) {
            if ((partner+"").equals(doctors.get(j))) {
                ag.setStatus(STATUS_DISABLE);
                uids.add(partner);
                ages.add(ag);
                break;
            }
        }
    }
    if (isEmpty(ages)) {
        msg = new Message(30041, "请选择要删除的人员");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
    DBTools.db("basedb").modItem(ags);
    final Map<Integer, User> users = usersByUids(uids);
    MyTask task = new MyTask(ages.size());
    for (int i = 0; i < ages.size(); i++) {
        ag = ages.get(i);
        task.runTask(task.new MyRunnable(task.new DBOperate() {

				@Override
				public Object operate(Object... objects) {
                    Arrangement ag = (Arrangement)objects[0];
                    Information info = new Information();
                    info.setOid(ag.getId()+"");
                    info.setHospital(ag.getHospital());
                    info.setTitle("日程通知");
                    info.setSumary("日程");
                    User user =  users.get(ag.getUid());
                    String author = user.getName();
                    user =  users.get(ag.getPartner());
                    String joiner = user.getName();
                    info.setContent(joiner + "您好, "+author+"把您移除了日程。\n内容:"+ag.getTitle()+"\n日期:"+ag.getStart());
                    info.setCategory(4);
                    createMsg(info, ag.getPartner());
					return null;
				}
			}, i, new Object[]{ag}));
    }
    task.stopTask();
    Tools.encryptPrint(out, msg);
%>
